﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;

namespace PrinterBudi.Persistence
{
    public class PBEmployeeModel
    {
        public static void Delete(PBEmployee existingEmployee)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(existingEmployee);
                    transaction.Commit();
                }
            }
        }

        public static void Update(PBEmployee newEmployeeName)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(newEmployeeName);
                    transaction.Commit();
                    Console.WriteLine("Updated Employee: " + newEmployeeName.Id);
                }
            }
        }

        public static PBEmployee Read(string employeePhoneNumber)
        {
            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    var employeeQuery = (from employee in session.Query<PBEmployee>()
                                         where employee.PhoneNumber == employeePhoneNumber
                                         select employee).Single();

                    return employeeQuery;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        public static List<PBEmployee> ReadAllEmployee()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var employeeQuery = (from employeetemp in session.Query<PBEmployee>()
                                 select employeetemp).ToList();

                Console.WriteLine("Read User: " + employeeQuery.Count);
                return employeeQuery;
            }
        }

        public static bool Create(string phonenumber, string password, string name)
        {
            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        if (Read(phonenumber) != null)
                        {
                            Console.WriteLine(Read(phonenumber).Id.ToString());
                            return false;
                        }

                        var employee = new PBEmployee
                        {
                            PhoneNumber = phonenumber,
                            Password = password,
                            Name = name,
                        };
                        session.Save(employee);

                        transaction.Commit();
                        Console.WriteLine("Created Employee: " + employee.Id);

                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}
